/*
 *  Kara IM - Kara Instant Messenger
 *  Copyright (C) 2007-2008 Markus Karnik
 *
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 3 of the License, 
 *  or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along 
 *  with this program; if not, see <http://www.gnu.org/licenses/>.
 *  
 */

package de.karnik.karaim;

import de.karnik.helper.LogFunctions;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

/**
 * This class implements the chat window activity.
 * 
 * @author Markus Karnik - markus.karnik@my-design.org
 * @version 1.0
 * @since 1.0
 */
public class KaraIMConfigWindow extends Activity implements OnClickListener {

	/**
	 * The protocol types.
	 */
	String[] entrys = {"XMPP","ICQ", "AIM", "GaduGadu" };
    /**
     * The username input field.
     */
    EditText usernameWidget;
    /**
     * The password input field.
     */
    EditText passwordWidget;
    /**
     * The server input field.
     */
    EditText serverWidget;
    /**
     * The port input field.
     */
    EditText portWidget;
    /**
     * The resource input field.
     */
    EditText resourceWidget; 
    /**
     * The tls checkbox.
     */
    CheckBox tlsWidget;
    /**
     * The save button.
     */
    Button save;
	
    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    protected void onCreate( Bundle icicle ) {
        super.onCreate( icicle );

        LogFunctions.log( "KaraIMConfigWindow.onCreate", "startup" );
        
		// get the settings file editor
        SharedPreferences settings = getSharedPreferences( "androidKaraIM", MODE_WORLD_READABLE );
        Editor settingsEditor = settings.edit();
        settingsEditor.commit();
        
        final Intent intent = getIntent();

        setContentView( R.layout.layout_config1 );
        
        LogFunctions.log( "KaraIMConfigWindow.onCreate", "get views" );
        
        // get views
        usernameWidget = ( EditText )findViewById( R.id.username );
        passwordWidget = ( EditText )findViewById( R.id.password );
        serverWidget = ( EditText )findViewById( R.id.server );
        portWidget = ( EditText )findViewById( R.id.port );
        resourceWidget = ( EditText )findViewById( R.id.resource ); 
        tlsWidget = ( CheckBox )findViewById( R.id.tls );
        save = ( Button )findViewById( R.id.saveButton1 );
        
        save.setOnClickListener( this );
        
        LogFunctions.log( "KaraIMConfigWindow.onCreate", "read data1" );
        // read contents and set to views
        usernameWidget.setText( settings.getString( "karaimUsername", "" ) );
        passwordWidget.setText( settings.getString( "karaimPassword", "" ) );
        serverWidget.setText( settings.getString( "karaimServer", "jabber.org" ) );
        portWidget.setText( String.valueOf( settings.getInt( "karaimPort", 5222 ) ) );
        resourceWidget.setText( settings.getString( "karaimResource", "Android" ) );
        tlsWidget.setChecked( settings.getBoolean( "karaimTls", true ) );
        
	}

	/* (non-Javadoc)
	 * @see android.view.View$OnClickListener#onClick(android.view.View)
	 */
	public void onClick( View view ) {

		// if clicked view is save
		if( view.equals( save ) ){

			// get the settings file editor
	        SharedPreferences settings = getSharedPreferences( "androidKaraIM", MODE_WORLD_READABLE );
	        Editor settingsEditor = settings.edit();
	    
	        // write the settings to the settings file
	        settingsEditor.putString( "karaimUsername", usernameWidget.getText().toString() );
	        settingsEditor.putString( "karaimPassword", passwordWidget.getText().toString() );
	        settingsEditor.putString( "karaimServer", serverWidget.getText().toString() );
	        settingsEditor.putInt( "karaimPort", Integer.parseInt( portWidget.getText().toString() ) );
	        settingsEditor.putString( "karaimResource", resourceWidget.getText().toString() );
	        settingsEditor.putBoolean( "karaimTls", tlsWidget.isChecked() );
	        
	        settingsEditor.commit();
		}	
	}
}